<template>
  <!-- 车费明细 -->
  <div class="app-container">
    <div style="width: 100%; float: left; " id="divSearch" ref="divSearch">
      <el-form :inline="true" :model="Search" size="mini" class="demo-form-inline">
        <el-form-item label="发放月份：" prop="Search">
          <el-date-picker v-model="Search.SendMonth" type="month" value-format="yyyy-MM" placeholder="选择月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单位名称：" prop="Search">
          <el-select v-model="Search.Organizeid" clearable placeholder="单位" style="width: 100%; display: flex;" @change="selectChange">
            <el-option v-for="item in OrganizeList" :key="item.Organizeid" :label="item.Organizename" :value="item.Organizeid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="Search">
          <el-select v-model="Search.Status" placeholder="请选择">
            <el-option v-for="item in cities" :key="item.value" :label="item.label" :value="item.value">
              <span style="float: left">{{ item.label }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getDataList">查询</el-button>
          <el-button type="success" icon="el-icon-circle-plus-outline" @click="toAdd" size="mini">新增</el-button>
          <el-button type="warning" icon="el-icon-download" size="mini">导出</el-button>
          <!-- <el-button type="warning" icon="el-icon-download" @click="calculation" size="mini">计算</el-button> -->

        </el-form-item>
      </el-form>
    </div><br />
    <div id="father" style="width: 100%;height: 700px; margin-top: 0px;float: left;">

      <el-table v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)" :data="datalist" style="width: 100%;margin-top:30px;" size="mini" border @sort-change="sortChange">
        <!--  -->
        <el-table-column align="center" label="车费补贴单号">
          <template slot-scope="scope">
            {{ scope.row.TemporaryfareCostNo }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="发放月份">
          <template slot-scope="scope">
            {{ scope.row.SendMonth }}
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="制造群">
          <template slot-scope="scope">
            {{ scope.row.Organizeid }}
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="单位名称">
          <template slot-scope="scope">
            {{ scope.row.Organizename }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="输送机构">
          <template slot-scope="scope">
            {{ scope.row.Conveyingmechanism }}
          </template>
        </el-table-column>
        <el-table-column align="header-center" label="审核状态">
          <template slot-scope="scope">
            {{ scope.row.BillStateName }}
          </template>
        </el-table-column>
        <el-table-column align="header-center" label="当前审核人">
          <template slot-scope="scope">
            {{ scope.row.NextAduitManName }}
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="所属组织">
          <template slot-scope="scope">
            {{ scope.row.OwnOrganizeID }}
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="getDetail(scope)" size="mini" circle></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="handleDelete(scope)" size="mini" circle></el-button>
          </template>
        </el-table-column>
      </el-table>



  <!--    <div style="margin-top: 10px;">
        <el-form :inline="true" :model="formInline" label-width="100px" label-position="left" class="demo-ruleForm">
          <div style="width: 50%;float: left;">
            <el-form-item label="合作协议:">
              <template>
                <el-link @click.native="open()" type="primary">链接到合作协议登记表</el-link>
              </template>
            </el-form-item>
          </div>



        </el-form>




      </div> -->
      <div id="son" style="float: right;">
        <el-pagination background layout="sizes, prev, pager, next,total" :page-size="Search.pageSize" :total="Search.total"
          @current-change="pageChange" />
      </div>


    </div>
  </div>
</template>

<script>
  import path from 'path'
  import {
    deepClone
  } from '@/utils'
  import {
    getDataList,
    updateCarFareHead,
    deleteHeadAndBody,
    updateCarFareBody,
    updateAttendanceCar,
    getWorkerByOrganize,
    deleteDetail
  } from '@/api/school'
  import {
    calculateCarCost
  } from '@/api/enterprise'
  import {
    getSelectList
  } from '@/api/getSelect'
  // 默认的编辑字段
  const defaultObject = {

  }
  export default {

    data() {
      return {
        cities: [{
          value: '',
          label: '请选择'
        }, {
          value: '0',
          label: '草稿'
        }, {
          value: '1',
          label: '审核中'
        }, {
          value: '2',
          label: '结案'
        }, {
          value: '3',
          label: '作废'
        }],
        value: '',
        //用于计算的临时数组
        headlist: [],
        form: {},
        bodylist: [],
        loading: false,
        OrganizeList: [],
        dialogVisible: false,
        EditObj: Object.assign({}, defaultObject),
        //测试
        money: null,
        datalist: [],
        datalistNopage: [],
        allSum: 0,
        Search: { // 查询条件
          total: 0, // 总记录数
          pageSize: 10, // 一页显示的记录数
          page: 1, // /当前页码
          RNO: '011', // 表编号  根据Controller中对应的表编号
          jsWhere: '' // 自定义条件
        },
        SearchBody: { // 查询条件
          RNO: '005', // 表编号  根据Controller中对应的表编号
          jsWhere: '' // 自定义条件
        },
        SearchApproval: { // 查部门
          total: 0, // 总记录数
          pageSize: 100, // 一页显示的记录数
          RNO: '002', // 表编号  根据Controller中对应的表编号
          jsWhere: '' // 自定义条件
        }
      }
    },
    computed: {

    },
    created() {
      this.getDataList()
      this.getSelect()
    },
    methods: {
      async getSelect() { // 获取下拉
        const res2 = await getSelectList({
          data: JSON.stringify(this.SearchApproval)
        })
        this.OrganizeList = res2.data.Table
      },
      //计算
      async calculation() {
        if(this.Search.SendMonth =="" || this.Search.SendMonth == null || this.Search.Organizeid =="" || this.Search.Organizeid == null){
          this.$message({
                    message: '请选择发放月份和单位',
                    type: 'warning'
                  });
        }else{
          this.loading = true
          this.Search.Status = 0
          const resHead = await getDataList({
            data: JSON.stringify(this.Search)
          })
          this.headlist = resHead.data.Table
          for (let i = 0; i < this.headlist.length; i++) {

            this.form = this.headlist[i];
            this.SearchBody.TemporaryCostNo = this.headlist[i].TemporaryfareCostNo
              //先清数据
              const resDelete = await deleteDetail({
                billID: this.SearchBody.TemporaryCostNo,
                dataBase: "TLC_TemporaryfareCostDetail",
                name: "TemporaryCostNo"
              })
              if (resDelete.code === 20000) {
                //获取数据
                const res3 = await getWorkerByOrganize({
                  billID: this.Search.Organizeid,
                  UserType: 2
                })
                this.bodylist = res3.data.Table;
                this.form.DeliverType = 2
                const result = await updateCarFareBody({
                  BodyDate: JSON.stringify(this.bodylist),
                  HeadDate: JSON.stringify(this.form),
                  billID: this.SearchBody.TemporaryCostNo,
                  IsPostBack: true,
                  submit: 0,
                });
                if (result.code == "20000") {
                  const resAttendance = await updateAttendanceCar({
                    billID: this.SearchBody.TemporaryCostNo,
                    sendDate: this.Search.SendMonth
                  })
                  if (resAttendance.code === 20000) {
                      const resCost = await calculateCarCost({
                        billID: this.SearchBody.TemporaryCostNo
                      })
                  }
                } else {
                  alert(result.code)
                }
              }
          }
          this.loading=false
        }
        this.getDataList()
      },


      toAdd() {
        let routeUrl = this.$router.push({
          path: "/enterprise/FareBodyE"
        });
        window.open(routeUrl.href, '_top');
      },
      //查看明细带管理费单号
      async getDetail(scope) {
        let routeUrl = this.$router.push({
          path: "/enterprise/FareBodyE",
          query: { billID: scope.row.TemporaryfareCostNo }
        });
        window.open(routeUrl.href, '_top');
      },

      //合作协议 链接跳转
      open() {
        this.$router.push({
          path: '/CooperationAgreement'
        })
      },
      pageChange(page) { // 翻页
        this.Search.page = page
        this.getDataList()
      },
      async getDataList() { // 获取列表数据，通用
        this.Search.DeliverType = 2
        const res = await getDataList({
          data: JSON.stringify(this.Search)
        })
        this.datalist = res.data.Table //将data 中的数据放到queueList中
        this.Search.total = res.data.Table1[0].RecordCount

      },
      async confirmData() { // 提交数据，需要修改
        const isEdit = this.dialogType === 'edit'

        if (isEdit || true) {
          const result = await updateCarFareHead({
            head: JSON.stringify(this.EditObj),
            billID: this.EditObj.TemporaryfareCostNo,
            IsPostBack: true
          })

          

          if (result.code === 20000) {
            const {
              description,
              key,
              name
            } = this.EditObj
            this.dialogVisible = false
            this.$notify({
              title: 'Success',
              dangerouslyUseHTMLString: true,
              message: result.message,
              type: 'success'
            })
            this.getDataList()

          } else {
            this.$notify({
              title: 'error',
              dangerouslyUseHTMLString: true,
              message: result.message,
              type: 'error'
            })
          }
        }
      },
      async handleDelete({
        $index,
        row
      }) {
        this.$confirm('确定删除该记录吗?', '警告', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(async () => {
            await deleteHeadAndBody({
              ID: JSON.stringify(row.TemporaryfareCostNo),
              status: 'car'
            })
            this.datalist.splice($index, 1)
            if (result.code === 20000) {
              this.dialogVisible = false
              this.$notify({
                title: 'Success',
                dangerouslyUseHTMLString: true,
                message: result.message,
                type: 'success'
              })
              this.getDataList()
            } else {
              this.$notify({
                title: 'error',
                dangerouslyUseHTMLString: true,
                message: result.message,
                type: 'error'
              })
            }

          })

      },


    }
  }
</script>

<style>
  #father {
    position: relative;
  }

  #son {
    position: absolute;
    right: 1%;
    top: 92%;
  }
</style>
